<template>
  <view class="margin-sm">
<!--    <cu-custom bgColor="bg-yellow" :isBack="true"><block slot="backText">返回</block>-->
<!--      <block slot="content">首页</block>-->
<!--    </cu-custom>-->
    <view class="cu-bar bg-white search fixed" style="top:0px;display: none">
      <view class="search-form round">
        <text class="cuIcon-search"></text>
        <input type="text" placeholder="搜索cuIcon" confirm-type="search" @input="searchIcon" />
      </view>
    </view>
<!--    style="margin-top: 50px"-->
<!--    @click="VerificateOrder(item.orderId)"-->
    <view class="cu-list menu">
      <view class="cu-item" v-for="(item,idx) in list" :key="idx">
        <view class="margin-right-sm">
          <checkbox class='round blue checked' @click="checkedList(item.orderId)"></checkbox>
        </view>
        <view class="content">
          <view class="text-grey"> {{item.poiName}} </view>
          <view class="text-gray text-sm flex">
            <view class="text-cut">
<!--              <text class="cuIcon-infofill text-red  margin-right-xs"></text>-->
              {{item.productName}}
            </view>
          </view>
        </view>
        <view class="action">
          <view class="text-grey text-xs">{{item.payTime}}</view>
          <view class="cu-tag round bg-grey sm">￥{{item.orderPrice}}</view>
        </view>
      </view>
    </view>
    <button class="cu-btn block bg-blue margin-tb-sm lg" @tap="verificateOrder" v-if="list.length > 0">核销</button>
    <!--    <scroll-view class="bg-white nav">-->
<!--      <ul class="vul">-->
<!--        <li class="vli" v-for="(item,idx) in orderList" :key="idx">-->
<!--          <view>-->
<!--            <view style="display:flex;">-->
<!--              <view>-->
<!--                {{item.poiName}}-->
<!--              </view>-->
<!--              <view style="flex: 9;text-align: right">-->
<!--                金额:{{item.payAmount}}元-->
<!--              </view>-->
<!--            </view>-->
<!--            <view>-->
<!--              {{item.productName}}-->
<!--            </view>-->
<!--          </view>-->
<!--        </li>-->
<!--      </ul>-->
<!--      <view class="cu-load bg-blue" style="display: none" :class="!isLoad?'loading':'over'"></view>-->
<!--    </scroll-view>-->
  </view>
</template>

<script setup lang="ts">
import {onReachBottom,onLoad} from "@dcloudio/uni-app";
import {ref, onMounted, reactive, toRaw} from 'vue';
import {verificateOrderList,verifyOrder} from '../../common/api/order'
import {useStore} from "../../common/store";
const store = useStore();
const loadProgress = ref(1)
const  searchIcon = (e) => {
  let key = e.detail.value;
  console.log('key:',key)
}
const isLoad = ref(false)

const list = reactive([])

const formatDateTime = (date, format) => {
  console.log('data:',date)
  const o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
    'H+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    S: date.getMilliseconds(), // 毫秒
    a: date.getHours() < 12 ? '上午' : '下午', // 上午/下午
    A: date.getHours() < 12 ? 'AM' : 'PM', // AM/PM
  };
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(
          RegExp.$1,
          RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
      );
    }
  }
  return format;
}

let checkBoxList = []
const verificateOrder = () => {
  if (checkBoxList.length == 0) {
    uni.showToast({
      title: '请选择待核销订单!',
      duration: 2000,
      icon: 'none'
    });
    return
  }
  uni.showModal({
    title: '提示',
    content: '确定要核销这批订单吗？',
    showCancel: true, // 显示取消按钮
    cancelText: '取消',
    confirmText: '确定',
    success: (res) => {
      if (res.confirm) {
        let data = {}
        data['adminId'] = store.userId
        data['orderIds'] = checkBoxList
        verifyOrder(data).then(res=>{
          if (res.code == 200) {
            checkBoxList = []
            list.splice(0,list.length)
            loadList()
          }
        })
      } else if (res.cancel) {
        console.log('用户点击取消');
      }
    }
  });
}

onMounted(() => {
  loadList()
})


const checkedList = (id) => {
  console.log('id:',id)
  if(checkBoxList.includes(id)) {
    checkBoxList = checkBoxList.filter(function(item) {
      return item !== id
    });
  } else {
    checkBoxList.push(id)
  }
}



const  CheckboxChange = (e) => {
      console.log('e:',e.detail.value)
  // for (var i = 0, lenI = items.length; i < lenI; ++i) {
  //   items[i].checked = false;
  //   for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
  //     if (items[i].value == values[j]) {
  //       items[i].checked = true;
  //       break
  //     }
  //   }
  // }
}

const loadList = async () => {
  const id = store.userId?store.userId:0
  // const id = 7
  await verificateOrderList(id).then(res => {
    if (res.code == 200) {
      res.data.forEach(item=>{
        let time = item.order.payTime
        if(time) {
          time = time * 1000
          let date = new Date(time)
          console.log('data1:',date,time)
          time = formatDateTime(date,'HH:mm')
        }
        item['payTime'] = time
        list.push(item)
      })
    }
  })
}

onReachBottom(()=>{
  console.log('loading more')
})
</script>

<style lang="scss" scoped>
.btnV {
  position: fixed;
  bottom: 0px;
}
</style>
